package org.huang.sample15.service;

import org.huang.sample15.bo.Dept;
import org.huang.sample15.dao.DeptAnnoMapper
import org.huang.sample15.dao.DeptXmlMapper
import org.huang.sample15.util.DateUtil;
import spock.lang.Specification;

import org.mockito.*
import spock.lang.Timeout;

import java.util.concurrent.TimeUnit;

class DemoServiceTest extends Specification {



    def deptAnnoMapper = Mock(DeptAnnoMapper)
    def deptXmlMapper = Mock(DeptXmlMapper)

    def demoService =  Spy(DemoService).with {
        deptAnnoMapper = this.deptAnnoMapper
        deptXmlMapper = this.deptXmlMapper
        it
    }

    def setup() {
    }

    def "test list By Ids"() {
        given:
        deptAnnoMapper.getOne(_ as String) >>> [new Dept(code: "1", name: "xx部门"), new Dept(code: "2", name: "xx部门2")]
        when:
        List<Dept> result = demoService.listByIds(["1", "2"])
        then:
        result == [new Dept(code: "1", name: "xx部门"), new Dept(code: "2", name: "xx部门2")]
    }


    @Timeout(value = 500, unit=TimeUnit.MILLISECONDS)
    def "set method timeout"(){
       expect: demoService.testTimeout()
    }


    def "test method getDeptById"(){
        given:
        deptAnnoMapper.getById(_) >> new Dept(code:"1",name: "xx")
        deptXmlMapper.selectByPrimaryKey(_) >> null
        when:
        Dept result = demoService.getDeptById(1)
        then:
        result == new Dept(code:"1",name: "xx")
    }

    def "test void method "(){
        given:
        deptAnnoMapper.voidMethod(_) >> _
        when:
        demoService.testVoidMethod(1)
        then:
        1 * deptAnnoMapper.voidMethod(_)
    }

    def "test threadMethod"(){
        given:
        deptAnnoMapper.getOne(_) >> {String code -> new Dept(code:code)}
        def mockStatic = Mockito.mockStatic(DateUtil)
        mockStatic.when(DateUtil::getNow()).thenReturn("a")
        when:
        def result = demoService.threadMethod(["1","2","3"])
        then:
        result ==  [new Dept(code: "1"), new Dept(code: "2"), new Dept(code: "3")]
    }

    def "test staticMethod"(){
        given:
        def mockStatic = Mockito.mockStatic(DateUtil)
        mockStatic.when(DateUtil::getNow()).thenReturn("a")
        when:
        def result = demoService.staticMethod()
        then:
        result ==  "a"
    }
}

//Generated with love by TestMe :) Please report issues and submit feature requests at: http://weirddev.com/forum#!/testme